import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jc.common.OrderNumberGenerator;
import com.jc.constant.*;
import com.jc.util.HttpPostUtil;
import com.jc.util.R;
import com.jc.vo.ApiConfig;
import com.jc.vo.QuotaApplyVO;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Random;

public class QuotaApplyDemo {
    // jc提供的SecretId
    public static String SECRET_ID = "100001";
    // jc提供的SecretKey
    public static String SECRET_KEY = "a28aa7399a067919c913ead1b434552c";


    public static String SERVICE_HOST = "127.0.0.1:8080";

    public static String QUOTA_APPLY_URL = "http://" + SERVICE_HOST + "/jc-order/quotaApply/v1/apply";

    @Test
    public void testCreate() {
        QuotaApplyVO vo = getVo("NON_LC",OperationTypeEnum.NEW.getCode(),null,null); //申请

//        QuotaApplyVO vo = getVo("LC");

//        QuotaApplyVO vo = getVo("NON_LC",OperationTypeEnum.MODIFY.getCode(),"CI20250709092814001",null); //修改
//
//        QuotaApplyVO vo = getVo("NON_LC",OperationTypeEnum.CHANGE.getCode(),null,"CI20250709092814001"); //变更


        vo.setOperationType("MODIFY");
        vo.setCorpSerialNo("CI20250718164742001");

        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, QUOTA_APPLY_URL);
        try {

            ObjectMapper om = new ObjectMapper();
            om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            String response = HttpPostUtil.send(om.writeValueAsString(vo), config, false);
            R r = om.readValue(response, R.class);
            try {
                if (r.getSuccess()) {
                    QuotaApplyVO result = om.convertValue(r.getContent(), QuotaApplyVO.class);
                    r.setContent(result);
                }
            } catch (Exception e) {
                r.setSuccess(false);
                r.setCode(R.RCODE.SYS_ERROR.getCode());
                r.setMessage(R.RCODE.SYS_ERROR.getMessage());
                e.printStackTrace();
            }
            System.out.println(r.getMessage());
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    /**
     * 构建测试对象
     *
     * @param businessType
     * @return
     */
    private static QuotaApplyVO getVo(String businessType,String operationType, String corpSerialNo,String originalCorpSerialNo) {
        QuotaApplyVO vo = new QuotaApplyVO();

        vo.setBusinessType(businessType);
        vo.setOperationType(operationType);
        if (OperationTypeEnum.CHANGE.getCode().equalsIgnoreCase(vo.getOperationType())){
            vo.setChangeReasonCode("1");
            vo.setOriginalCorpSerialNo(originalCorpSerialNo);
        }

        if (OperationTypeEnum.MODIFY.getCode().equalsIgnoreCase(vo.getOperationType())){
            vo.setCorpSerialNo(corpSerialNo);
        }


        vo.setCreateUser("张三");
//        vo.setCorpSerialNo(OrderNumberGenerator.generate("CI"));
        vo.setRiskCompName(getRandomChineseString(5));
        vo.setCountryOrArea(CountryEnum.CHN.getCode());
        vo.setRiskCompAddress(getRandomChineseString(100));
        vo.setProvince(ProvinceEnum.ZHEJIANG.getCode());
        vo.setRiskPhone("15967899876");
        //买方注册号
        vo.setRiskMark(OrderNumberGenerator.generate("CHN"));
        vo.setPaidTerm(180);
        vo.setAppliAmount(new BigDecimal("50000"));
        vo.setReportType("1");
        vo.setExportTrade(ProductCategoryEnum.OTHER.getCode());
        if (ProductCategoryEnum.OTHER.getCode().equalsIgnoreCase(vo.getExportTrade())) {
            vo.setExportTradeInput("商品类别填写是其他， 商品类别说明 必填");
        }
        vo.setTransType(TransportModeEnum.SEA.getCode());
        vo.setRemark("备注");


        if ("LC".equalsIgnoreCase(businessType)) {

            vo.setCreditNo("TExt" + getRandomChineseString(8));
            vo.setIsCycleUse("1");
            vo.setBankName("测试银行");
            vo.setBankCountry(CountryEnum.CHN.getCode());
            vo.setBankSwift("BankSwift");
            vo.setBankAddress(" 信用证必填项开证行地址");
            vo.setIsBatch("1");
            if ("1".equalsIgnoreCase(vo.getIsBatch())) {
                vo.setMaxAmount1(new BigDecimal("25000"));
                vo.setMaxAmount2(new BigDecimal("25000"));
            }

            //出口商品名称(LC)
            vo.setExportComName("电子产品");
            vo.setPayWay("LC");
        }

        if ("NON_LC".equalsIgnoreCase(businessType)) {
            //出口商品名称(非LC)
            vo.setExportName("日用品");
            vo.setPayWay("OA");
            vo.setNeedTranslation("1");
        }


        //是否存在历史交易（0：否；1：是）
        vo.setHistoryBusiness("0");
        if ("1".equalsIgnoreCase(vo.getHistoryBusiness())) {
            vo.setEarlyDealYear("2019");
            vo.setRiskPerformance("及时");
            if ("LC".equalsIgnoreCase(businessType)) {
                vo.setTransactionLc("30000");
                vo.setBankPerformance("及时");
            }
//            vo.setTransactionDp();
            if ("NON_LC".equalsIgnoreCase(businessType)) {
                vo.setTransactionDa("4000");
                vo.setStartYear("2019");
            }

            //历史上有无拖欠？
            vo.setIsDefaultThis("1");
            if ("1".equalsIgnoreCase(vo.getIsDefaultThis())) {
                vo.setDefaultDate(60);
                vo.setDefaultAmount(new BigDecimal("10000"));
            }

        }
        return vo;


    }


    private static String getRandomChineseString(int length) {
//        StringBuilder sb = new StringBuilder();
//        Random random = new Random();
//        for (int i = 0; i < length; i++) {
//            // 生成Unicode编码范围内的随机码点，用于创建随机中文字符。
//            int codePoint = random.nextInt(0x9fff - 0x4e00 + 1) + 0x4e00;
//            // 将码点转换为字符，并添加到字符串构建器中。
//            char c = Character.toChars(codePoint)[0];
//            // 添加到字符串
//            sb.append(c);
//        }
//        // 返回构建完成的随机中文字符串。
//        return new String();
        String CHAR_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHAR_LIST.length());
            sb.append(CHAR_LIST.charAt(randomIndex));
        }
        return sb.toString();
    }


}
